package com.liugai.lgeachday.service;

import com.liugai.lgeachday.entity.User;
import com.liugai.lgeachday.mapper.UserMapper;
import com.liugai.lgeachday.redis.RedisManager;
import com.liugai.lgeachday.request.RequestCheck;
import com.liugai.lgeachday.request.Response;
import com.liugai.lgeachday.tool.EntityTool;
import com.liugai.lgeachday.tool.FileHandler;
import com.liugai.tool.token.TokenTicket;
import com.liugai.tool.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;


@Service
public class UserService implements BaseServiceInterface<User> {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public Response queryObject(int id) {
        User user = userMapper.selectByPrimaryKey(id);
        if (user == null) {
            return Response.emptyResultResponse(null);
        }
        return Response.successResponse(null, user);
    }

    public Response queryObject(String account) {
        Response response = RequestCheck.checkEmptyParam(account);
        if (response != null) return response;
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("account", account);
        User user = userMapper.selectOneByExample(example);
        if (user == null) {
            return Response.emptyResultResponse(null);
        }
        return Response.successResponse(null, user);
    }

    @Override
    public Response queryObjectList() {
        List<User> users = userMapper.selectAll();
        if (users == null) {
            return Response.emptyResultResponse(null);
        }
        return Response.successResponse(null, users);
    }

    @Override
    public Response updateObject(User obj) {
        List<String> list = new ArrayList<>();
        list.add("id");
        Response response = RequestCheck.checkMustParam(obj,list);
        if (response != null) return response;
        Example example = null;
        try {
            example = EntityTool.generateExample(obj, null, null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        int result = userMapper.updateByExampleSelective(obj, example);
        if (result>0) {
            return Response.successResponse(null,null);
        }
        return Response.failedResponse(null);
    }

    @Override
    public Response insertObject(User obj) {
        List<String> list = new ArrayList<>();
        list.add("account");
        list.add("password");
        Response response = RequestCheck.checkMustParam(obj,list);
        if (response != null) return response;
        User user = userMapper.selectByPrimaryKey(obj.getAccount());
        if (user != null) {
            return Response.existMutableResponse(null);
        }
        int result = userMapper.insert(obj);
        if (result>0) {
            return Response.successResponse(null,null);
        }
        return Response.failedResponse(null);
    }

    @Override
    public Response deleteObject(int id) {
        List<String> list = new ArrayList<>();
        list.add("id");
        User userParams = new User();
        userParams.setId(id);
        Response response = RequestCheck.checkMustParam(userParams,list);
        if (response != null) return response;
        User user = userMapper.selectByPrimaryKey(userParams.getId());
        if (user == null) {
            return Response.emptyParamResponse(null);
        }
        int result = userMapper.delete(userParams);
        if (result>0) {
            return Response.successResponse(null,null);
        }
        return Response.failedResponse(null);
    }

    public Response register(User user) {
        return this.insertObject(user);
    }

    public Response login(User user) {
        List<String> list = new ArrayList<>();
        list.add("account");
        list.add("password");
        Response response = RequestCheck.checkMustParam(user,list);
        if (response != null) return response;
        Example userExample = new Example(User.class);
        userExample.createCriteria().andEqualTo("account",user.getAccount()).andEqualTo("password",user.getPassword());
        User loginUser = userMapper.selectOneByExample(userExample);
        if (loginUser != null) {
            TokenTicket ticket = TokenUtil.generateToken(loginUser.getId().longValue());
            if (ticket != null) {
                redisManager.save(ticket.getToken(), Long.valueOf(loginUser.getId()), ticket.getExpDate());
                loginUser.setToken(ticket.getToken());
            }
            return Response.successResponse(null, loginUser);
        }
        return Response.emptyResultResponse(null);
    }

    public Response updateHead(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        System.out.println(fileHandler);
        String idStr = request.getParameter("id");
        Long id = Long.valueOf(idStr);
        Response response = fileHandler.uploadImage(file);
        if (response.getCode() == 200) {
            String headUrl = (String) response.getData();
            User user = new User();
            user.setId(id.intValue());
            user.setHeadurl(headUrl);
            Response result = updateObject(user);
            if (result.getCode() == 200) {
                return new Response(200,"头像更新成功", headUrl);
            }
            return  Response.failedResponse("头像更新失败");
        }
        return Response.failedResponse("图片上传失败");
    }
}
